/**
 * Created by Administrator on 2017/11/18.
 */
String.prototype.startWith = function (str) {
    var reg = new RegExp("^" + str);
    return reg.test(this);
}
myTrello.removeByValue = function (arr, val) {
    // for (var i = 0; i < arr.length; i++) {
    //     if (arr[i] == val) {
    //         arr.splice(i, 1);
    //         break;
    //     }
    // }
}
myTrello.filter=function(type)
{
    myTrello.filterTopCard(type);
    myTrello.generateHtml(type);
}
myTrello.filterTopCard = function (option) {
    console.log("filterTopCard");
    console.log("global.cards.length=" + global.cards.length);
    global.topCards=[];
    global.cards.forEach(function (card) {
        //console.log(card.name);
        var contains = false;
        card.labels.forEach(function (label) {
            if(option==CONSTANTS_ALL)
            {
                if (label.name == CONSTANTS_BUS || label.name == CONSTANTS_LABEL_ACCOUNT) {
                    contains = true;
                    return;
                }
            }
            else
            {
                if (label.name == option) {
                    contains = true;
                    return;
                }
            }

        });
        if (contains) {
            card.level=0;
            global.topCards.push(card);

            myTrello.removeByValue(global.cards, card);
        }

    });
    console.log("global.cards.length222=" + global.cards.length);
    console.log("global.topCards.length=" + global.topCards.length);
    global.topCards.forEach(function (topCard) {
        myTrello.addChildrenCard(topCard);
    });
};
//checklists
myTrello.addChildrenCard = function (card) {
    if (card.idChecklists != null) {
        card.checklist = [];
        //齐鲁银行账户体系
        card.idChecklists.forEach(function (idCheck) {
            global.checklists.forEach(function (checklist) {
                if (idCheck == checklist.id) {
                    checklist.checkItems.forEach(function (check) {
                        if (card.name == "齐鲁银行再保理业务") {
                            console.log("check.name=" + check.name);
                        }
                        var index = check.name.indexOf("https://trello.com/");
                        if (check.name.indexOf("https://trello.com/") != -1) {
                            check.type = "child";
                            check.childUrl = check.name.substr(index);
                            if (card.name == "齐鲁银行再保理业务") {
                                console.log("check.childUrl=" + check.childUrl);
                            }
                            check.name = check.name.substr(0, index);
                            if (check.childUrl != card.url) {
                            }
                            card.checklist.push(check);
                            var childCard = myTrello.getChild(check.childUrl);

                            if (childCard != null) {
                                if (card.childrenCards == null) {
                                    card.childrenCards = [];
                                }

                                childCard.level=card.level+1;
                                card.childrenCards.push(childCard);
                                myTrello.addChildrenCard(childCard);
                            }

                        }
                        else {
                            check.type = "item";
                            card.checklist.push(check);
                        }


                    });
                    myTrello.removeByValue(global.checklists, checklist);
                }
            });
        })
    }
};
myTrello.getChild = function (url) {
    var myCard = null;
    global.cards.forEach(function (card) {
        // if(cardName=="齐鲁银行再保理业务")
        // {
        //     console.log(card.name+" "+card.url);
        // }

        if (url.indexOf(card.shortUrl) != -1) {
            //myTrello.removeByValue(global.cards, card);
            myCard = card;
            return;
        }
    });
    return myCard;

};
myTrello.getListName = function (listId) {
    var listName = "";
    global.lists.forEach(function (listItem) {
        if (listItem.id == listId) {
            //console.log("listName=" + listItem.name);
            listName = listItem.name;
            return listName;
        }

    });
    return listName;
}
myTrello.generateHtml = function (type) {
    var pageTpl = getTemplate("tpl/report_form.tpl.html");
    var tableTpl = getTemplate("tpl/table.tpl.html");
    global.tableLineTpl = getTemplate("tpl/table_line.tpl.html");
    var exportCards = [];
    if (type == CONSTANTS_ALL) {
        exportCards = global.topCards;
    }
    else {
        global.topCards.forEach(function (card) {
            card.labels.forEach(function (label) {
                if (label.name == type) {
                    exportCards.push(card);
                    return;
                }
            });
        })
    }

    var tableHtml = myTrello.generateTableHtml(exportCards, type, tableTpl);
    var param = {};
    param.tableHtml = tableHtml;
    var page = Mustache.render(pageTpl, param);
    $("#view").html(page);
}
myTrello.generateTableHtml = function (exportCards, type, tableTpl,cardLevel) {
    var outCardList = [];
    for (var i = 0; i < exportCards.length; i++) {
        var card = exportCards[i];
        var outCard = myTrello.getContent(card.desc);
        //console.log("card.idList=" + card.idList);
        var listName = myTrello.getListName(card.idList);
        //console.log("-->list.Name:" + listName);
        outCard.remarks = listName;
        var progress = "";
        if (!outCard.empty) {
            for (var j = 0; j < outCard.progress.length; j++) {
                if (j > 0) {
                    progress = progress + CONSTANTS_SPLIT_;
                }
                progress = progress + outCard.progress[j];
            }
        }

            var members = "";
            card.idMembers.forEach(function (member) {
                var name = myTrello.getMemberName(member);
                members = members + " " + name;
            })
            outCard.manager = "负责人："+outCard.manager+ "<br>成员：" + members;



        outCard.detailList = null;
        if (card.checklist != null) {
            card.checklist.forEach(function (e) {

                var checkItem=e.name;
                if(e.state=='complete')
                {
                    checkItem="<div style='color: red'>"+checkItem+"</div>";
                }

                if (outCard.detailList == null) {
                    outCard.detailList = checkItem;
                }
                else {
                    outCard.detailList = outCard.detailList + checkItem;
                }
            });
        }
        if (card.childrenCards != null) {
            outCard.rowspan = 2;
            if (outCard.detailList == null) {
                outCard.detailList = myTrello.generateTableHtml(card.childrenCards, type, global.tableLineTpl,card.level);
            }
            else {
                outCard.detailList = outCard.detailList + "<br>" + myTrello.generateTableHtml(card.childrenCards, type, global.tableLineTpl,card.level);
            }

        }
        if (outCard.detailList == null || outCard.detailList == "") {
            outCard.rowspan = 1;
        }
        else {
            outCard.rowspan = 2;
        }
        outCard.rowid = i + 1;
        outCard.title = card.name;
        outCardList.push(outCard);
    }
    var param = {};
    param.bgHeaderColor=global.headerColors[cardLevel];
    param.bgRowColor=global.bodyRowColors[cardLevel];
    param.card = outCardList;
    var htmlString = Mustache.render(tableTpl, param);
    return htmlString;

}
myTrello.getContent = function (cardDescription) {
    var card = {
        "bankName": "",
        "manager": "",
        "progress": [],
        "planDate": "",
        "realDate": "",
        "remarks": "",
        "empty": true
    };
    var content = cardDescription.split("\n");
    if (content != null) {
        content.forEach(function (desc) {
            if (desc.startWith("对接银行:")) {
                card.bankName = desc.substr("对接银行:".length);
                card.empty = false;
            }
            else if (desc.startWith("对接人员:")) {
                card.manager = desc.substr("对接人员:".length);
                card.empty = false;
            }
            else if (desc.startWith("计划结束:")) {
                card.planDate = desc.substr("计划结束:".length);
                card.empty = false;
            }
            else if (desc.startWith("实际结束:")) {
                card.realDate = desc.substr("实际结束:".length);
                card.empty = false;
            }
            else if (desc.startWith("对接进度:")) {
                card.progress.push(desc.substr("对接进度:".length));
                card.empty = false;
            }
        });
    }
    return card;
}
myTrello.getMemberName = function (id) {
    var memberName = "";
    global.members.forEach(function (e) {
        if (e.id == id) {
            memberName = e.fullName;
        }
    });
    return memberName;
}